package benny.ApkposStore.Service;

//
//import com.tencent.elife.asyn.BaseManager;
//import com.tencent.elife.asyn.Command;
//import com.tencent.elife.asyn.DataResponse;

import java.util.HashMap;
import java.util.List;

import android.app.IntentService;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import benny.ApkposStore.db.ConfigDBHelper;
import benny.ApkposStore.model.Dish_class;
import benny.ApkposStore.model.Floor;
import benny.ApkposStore.model.Make_tbl;
import benny.ApkposStore.model.Room;
import benny.ApkposStore.model.View_dian;
import benny.ApkposStore.utils.ApkConfigdbUntils;
import benny.ApkposStore.utils.Apkglobal;
import benny.ApkposStore.utils.FileUtils;

public class ResponseService extends IntentService {

	final private int mSleepTime = 5000; // 10s
	final private String tag = "ResponseService";
	FileUtils filedb = new FileUtils();

	public ResponseService() {
		super("RoomstateService");
	}

	@Override
	protected void onHandleIntent(Intent intent) {

		while (true) {
			synchronized (this) {
				try {
					// Thread.sleep(100);

					if (filedb.isapkFileExist("shop_config.db", "databases/")) {
						updateresponse();	
					}				
					Thread.sleep(mSleepTime);
				} catch (Exception e) {
					Log.e(tag, e.getMessage());
				}
			}
		}
	}

	private void updateresponse() {
		// System.out.println("dddd");
		// //获取房台

		if (Apkglobal.getroomlist() == null) {
			List<Room> roomlist = ApkConfigdbUntils.updateroomlist(
					getApplicationContext());
			Apkglobal.setroomlist(roomlist);
		}

		// 获取楼层
		if (Apkglobal.getfloorlist() == null) {
			List<Floor> floorlist = ApkConfigdbUntils.updatefloorlist(
					getApplicationContext());
			Apkglobal.setfloorlist(floorlist);
		}

		// 获取菜品(大厅价)
		if (Apkglobal.getcainamelist() == null) {
			List<View_dian> cainamelist = ApkConfigdbUntils.updateViewDianList(
					 getApplicationContext(), "0");
			Apkglobal.setcainamelist(cainamelist);

		}
		// 获取菜品(包房价)
		if (Apkglobal.getcainamelist1() == null) {
			List<View_dian> cainamelist1 = ApkConfigdbUntils.updateViewDianList(
					 getApplicationContext(), "1");
			Apkglobal.setcainamelist1(cainamelist1);
		}

		// //////////////获取做法
		if (Apkglobal.getmaketbllist() == null) {
			List<Make_tbl> maketbllist = ApkConfigdbUntils.updatemakelist(
					getApplicationContext());
	
			Apkglobal.setmaketbllist(maketbllist);
		}

		// ////获取一级类别
		if (Apkglobal.getdishtypelist1() == null) {
			List<Dish_class> dishtypelist1 = ApkConfigdbUntils.updateClasslist(
					getApplicationContext(),0);
			Apkglobal.setdishtypelist1(dishtypelist1);

		}
		// ////获取二级类别
		if (Apkglobal.getdishtypelist2() == null) {
			List<Dish_class> dishtypelist2 = ApkConfigdbUntils.updateClasslist(
					getApplicationContext(),1);
			Apkglobal.setdishtypelist2(dishtypelist2);
		}
		///获取打印机IP
		if (Apkglobal.getprint_ipmap()==null) {
			HashMap<String, String> print_ipmap= ApkConfigdbUntils.getprint_ipmap(
					getApplicationContext());
			Apkglobal.setprint_ipmap(print_ipmap);
		}
		

		

		
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		// DataManager.instance().release();
	}

	@Override
	public void onStart(Intent intent, int startId) {
		// TODO Auto-generated method stub
		super.onStart(intent, startId);
		// DataManager.instance().init(getApplicationContext());
		System.out.println("start");
	}

}
